/**
 * Package: App
 * Class: App.Util.Position
 * @description: 绝对定位。A是参照物，B是弹出div，B需要在A的合理位置弹出。
 * @author: Allen.xu
 * @date: 2012-10-24 上午10:29
 * @lastUpdate: 2012-10-24 上午10:29
 */

(function(){
	Das.ns("App.Util.Position");
	Das.apply( App.Util.Position, {
		el: null,
		popEl: null,
		position: {
			x: 0,
			y: 0
		},

		/*
		 * 初始化
		 * @param {Object} json格式对象
		 * {
		 * 		el 触发弹出div的元素
		 * }
		 */
		init: function(data){
			var w = this;
			w.el = data.el;
			w.popEl = data.popEl;

            w.bodyAppend = data.bodyAppend || 1;

			w.popEl.css({
				"position": "absolute"
			});

			w.setPopPosition();
		},

		/**
		 * 设置弹出div的绝对位置
		 */
		setPopPosition: function(){
			var w = this,
				body = $(document.body),
				outDom = w.getOffsetParent();

			var p = w.getPostionObject();

			w.position.x = p.left;
			w.position.y = p.top + p.height;


			//弹出菜单需要在popBtn的上方展现
			if( p.top + p.height + p.popHeight > p.bodyHeight + p.docScrollTop && p.top - p.popHeight > 0){
				w.position.y = p.top - p.popHeight;
			}

			//弹出菜单需要在popBtn的右边展现（右边对齐）
			if( p.left + p.popWidth > p.docWidth ){
				w.position.x = p.left - ( p.popWidth - p.width );
			}

			//alert( $(document).height() + "\n\nbody:" + $(document.body).height() + "\n\ntop:" + p.top + "\n\nscr:" + p.docScrollTop)
            var left = w.position.x,
                top = w.position.y;

            if(w.bodyAppend == 0){
                left = w.position.x - outDom.offset().left// + outDom.scrollLeft();
                top = w.position.y - outDom.offset().top// + outDom.scrollTop();
            }
			w.popEl.css({
				'left': w.position.x,// - outDom.offset().left,// + outDom.scrollLeft(),
				'top': w.position.y// - outDom.offset().left// + outDom.scrollTop() - p.docScrollTop
			});
		},

		/****/
		getPostionObject: function(dom, popDom){
			var d = dom || this.el,
				pop = popDom || this.popEl;

			return {
				top: d.offset().top,
				left: d.offset().left,
				width: d.outerWidth(),
				height: d.outerHeight(),
				popWidth: pop.outerWidth(true),
				popHeight: pop.outerHeight(true),

				bodyHeight: $(document.body).height(),
				docHeight: $(document).height(),
				docWidth: $(document).width(),
				docScrollTop: $(document).scrollTop(),
				docScrollLeft: $(document).scrollLeft()
			}
		},

		/**
		 * 绝对定位过程中，获得父节点
		 * @param {Object} javascript dom
		 */
		getOffsetParent: function(dom){

			var dom = dom || this.el.get(0);
			var offsetParent = dom || document.body;

			//在IE浏览器中，某个标签含有下面三个CSS属性会导致position:relative;的效果。
			var cssAttr = ['overflow', 'overflow-x', 'overflow-y']

			while (
				offsetParent
					&& (
					!/^body|html$/i.test(offsetParent.tagName)
						&& jQuery.css(offsetParent, 'position') == 'static'
					)
				){
				var t = false;
				jQuery.each( cssAttr, function(i,name){

					if(jQuery.css(offsetParent, name) == 'auto' || jQuery.css(offsetParent, name) == 'scroll'){
						t = true;
						return;
					}

				} );
				if (t) break;
				offsetParent = offsetParent.offsetParent;
			}

			return $( offsetParent );
		}

	} );

})();